//
//  JiaYuanShareData.m
//  OneToOne
//
//  Created by Herui on 10/17/14.
//  Copyright (c) 2014 jiayuan. All rights reserved.
//

#import "JiaYuanShareData.h"
#import "HttpRequestManager.h"
#import "JYInter.h"

#define Hongniang_Detail @"红娘推荐详细页"


static NSString *const kSSKeychainServiceName = @"JiaYuanService";


@implementation JiaYuanShareData
{
    NSString *_uid;
    NSString *_token;
    NSString *_shopId;
    NSString *_emp_rname;
    NSString *_emp_name;
    NSString *_address;
    NSString *_emp_city;
    NSString *_interviewStatus;
    NSString *_bankCardNumber;
    NSString *_idCard;
}

+ (JiaYuanShareData *)sharedInstance
{
    static JiaYuanShareData *shareData = nil;
    if (shareData == nil) {
        shareData = [[JiaYuanShareData alloc] init];
    }
    return shareData;
}

- (id)init
{
    self = [super init];
    if (self) {
        //
        // self.deviceInfo = [[JYDeviceInfo alloc] init];
    }
    return self;
}

- (NSString *)uid
{
    if (_uid == nil || _uid.length == 0 || [_uid isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *uid = [ud objectForKey:kUDJYUserID];
        if (uid) {
            // _uid = @"6884";
             _uid = uid;
        } else {
             _uid = @"0";
            // _uid = @"6884";
            // uid=6884&token=2ff9161f083f3b7ad3c94cf248174ba4&
        }
    }
    return _uid;
}

- (void)setUid:(NSString *)uid
{
    _uid = uid;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:uid forKey:kUDJYUserID];
    [ud synchronize];
}

- (void)setToken:(NSString *)token
{
    _token = token;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:token forKey:kUDJYUserToken];
    [ud synchronize];
}

- (NSString *)token
{
    if (_token == nil || _token.length == 0 || [_token isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *token = [ud objectForKey:kUDJYUserToken];
        if (token) {
            // _token = @"2ff9161f083f3b7ad3c94cf248174ba4";

             _token = token;
        } else {
            // TOKO
             _token = @"";
            // _token = @"2ff9161f083f3b7ad3c94cf248174ba4";
        }
    }
    return _token;
}


- (NSString *)shopId
{
    if (_shopId == nil || _shopId.length == 0 || [_shopId isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *shopId = [ud objectForKey:KUDJYShopInfoID];
        if (shopId) {
            _shopId = shopId;
        } else {
            // TOKO
            _shopId = @"";
        }
    }
    return _shopId;
}

- (void)setShopId:(NSString *)shopId
{
    if (!shopId || [shopId isKindOfClass:[NSNull class]]) {
        return;
    }
    _shopId = shopId;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:shopId forKey:KUDJYShopInfoID];
    [ud synchronize];
}


- (NSString *)interviewStatus
{
    if (_interviewStatus == nil || _interviewStatus.length == 0 || [_interviewStatus isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *interviewStatus = [ud objectForKey:KUDInterviewStatus];
        if (interviewStatus) {
            _interviewStatus = interviewStatus;
        } else {
            _interviewStatus = @"1";
        }
    }
    return _interviewStatus;
}

- (void)setInterviewStatus:(NSString *)interviewStatus
{
    if (!interviewStatus || [interviewStatus isKindOfClass:[NSNull class]]) {
        return;
    }
    _interviewStatus = interviewStatus;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:interviewStatus forKey:KUDInterviewStatus];
    [ud synchronize];

}

- (NSString *)emp_rname
{
    if (_emp_rname == nil || _emp_rname.length == 0 || [_emp_rname isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *emp_rname = [ud objectForKey:KUDEmpRealName];
        if (emp_rname) {
            _emp_rname = emp_rname;
        } else {
            _emp_rname = @"";
        }
    }
    return _emp_rname;
}

- (void)setEmp_rname:(NSString *)emp_rname
{
    if (!emp_rname || [emp_rname isKindOfClass:[NSNull class]]) {
        return;
    }
    _emp_rname = emp_rname;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:emp_rname forKey:KUDEmpRealName];
    [ud synchronize];
    
}

- (NSString *)emp_name
{
    if (_emp_name == nil || _emp_name.length == 0 || [_emp_name isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *emp_name = [ud objectForKey:KUDEmpLoginName];
        if (emp_name) {
            _emp_name = emp_name;
        } else {
            _emp_name = @"";
        }
    }
    return _emp_name;
}

- (void)setEmp_name:(NSString *)emp_name
{
    if (!emp_name || [emp_name isKindOfClass:[NSNull class]]) {
        return;
    }
    _emp_name = emp_name;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:emp_name forKey:KUDEmpLoginName];
    [ud synchronize];
}

- (NSString *)emp_city
{
    if (_emp_city == nil || _emp_city.length == 0 || [_emp_city isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *emp_city = [ud objectForKey:KUDEmpCity];
        if (emp_city) {
            _emp_city = emp_city;
        } else {
            _emp_city = @"";
        }
    }
    return _emp_city;
}


- (void)setEmp_city:(NSString *)emp_city
{
    if (!emp_city || [emp_city isKindOfClass:[NSNull class]]) {
        return;
    }
    _emp_city = emp_city;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:emp_city forKey:KUDEmpCity];
    [ud synchronize];
}

- (NSString *)bankCardNumber
{
    if (_bankCardNumber == nil) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *bankCardNumber = [ud objectForKey:KUDBankNumber];
        if (bankCardNumber) {
            _bankCardNumber = bankCardNumber;
        } else {
            _bankCardNumber = @"";
        }
    }
    return _bankCardNumber;
}

- (void)setBankCardNumber:(NSString *)bankCardNumber
{
    if (!bankCardNumber || [bankCardNumber isKindOfClass:[NSNull class]]) {
        return;
    }
    _bankCardNumber = bankCardNumber;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:bankCardNumber forKey:KUDBankNumber];
    [ud synchronize];
}

- (NSString *)address
{
    if (_address == nil || _address.length == 0 || [_address isEqualToString:@""]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *address = [ud objectForKey:KUDEmpAddress];
        if (address) {
            _address = address;
        } else {
            _address = @"";
        }
    }
    return _address;
}

- (NSString *)idCard
{
    if (_idCard == nil) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSString *idCard = [ud objectForKey:KUDIDCard];
        if (idCard) {
            _idCard = idCard;
        } else {
            _idCard = @"";
        }
    }
    return _idCard;
}

- (void )setIdCard:(NSString *)idCard
{
    if (!idCard || [idCard isKindOfClass:[NSNull class]]) {
        return;
    }
    _idCard = idCard;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:idCard forKey:KUDIDCard];
    [ud synchronize];
}

- (void)setAddress:(NSString *)address
{
    if (!address || [address isKindOfClass:[NSNull class]]) {
        return;
    }
    _address = address;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:address forKey:KUDEmpAddress];
    [ud synchronize];
}

+ (BOOL)setPassword:(NSString *)password account:(NSString *)account
{
    // account 存入本地
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:account forKey:kUDJYAccount];
    [ud synchronize];
    
    // return [SSKeychain setPassword:password forService:kSSKeychainServiceName account:account];
    return YES;
}

+ (BOOL)deleteCurrentAccount
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *account = [ud objectForKey:kUDJYAccount];
    
    [ud removeObjectForKey:kUDJYUserID];
    [ud removeObjectForKey:kUDJYUserToken];
    [ud removeObjectForKey:KUDJYShopInfoID];
    [ud synchronize];
    
    [JiaYuanShareData sharedInstance].uid = @"0";
    [JiaYuanShareData sharedInstance].token = @"";
    [JiaYuanShareData sharedInstance].shopId = @"";
    [JiaYuanShareData sharedInstance].interviewStatus = @"";
    [JiaYuanShareData sharedInstance].idCard = @"";
    [JiaYuanShareData sharedInstance].bankCardNumber = @"";
    [JiaYuanShareData sharedInstance].emp_city = @"";
    [JiaYuanShareData sharedInstance].emp_rname = @"";
    [JiaYuanShareData sharedInstance].emp_name = @"";
    [JiaYuanShareData sharedInstance].address= @"";
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [[SDBManager defaultDBManager] close];
//    });
//    return [SSKeychain deletePasswordForService:kSSKeychainServiceName account:account];
    // return YES;
    return YES;
}
+ (NSString *)currentAccount
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *account = [ud objectForKey:kUDJYAccount];
    return account;
}

+ (NSString *)passwordForCurrentAccount
{
//    NSString *account = [[self class] currentAccount];
//    return [SSKeychain passwordForService:kSSKeychainServiceName account:account];
    return nil;
    
}

#pragma mark- 汉字转拼音和特殊字符显示
+ (NSString *)stringToSortOrSpecialSymbol:(NSString *)value
{
    CFStringRef theString = (__bridge CFStringRef)(value);
    CFMutableStringRef pinyinString = CFStringCreateMutableCopy(NULL, 0, theString);
    CFStringTransform(pinyinString, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform(pinyinString, NULL, kCFStringTransformStripDiacritics, NO);
    NSString *strK =[NSString stringWithFormat:@"%@",pinyinString];
    NSString *newPinYin = [[[[strK stringByReplacingOccurrencesOfString:@" " withString:@""]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    [newPinYin stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    
    NSString *py;
    // DLog(@"[newPinYin substringToIndex:1] = %@ , %ld ",[newPinYin substringToIndex:1],(long)[[newPinYin substringToIndex:1] integerValue]);
    
    if ([[newPinYin substringToIndex:1] integerValue]>0){
        py  = [NSString stringWithFormat:@"#%@",newPinYin];
    }else{
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
        
        NSString *filter = [[[newPinYin substringToIndex:1] componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL bTest = [[newPinYin substringToIndex:1] isEqual:filter];
        
        if (!bTest) {
            py  = [NSString stringWithFormat:@"#%@",newPinYin];
        }else{
            py  = [NSString stringWithFormat:@"%@",newPinYin];
        }
    }
    return  py;
}

#pragma mark- 验证身份证
+ (BOOL)validateIDCardNumber:(NSString *)value {
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSInteger length =0;
    if (!value) {
        return NO;
    }else {
        length = value.length;
        
        if (length !=15 && length !=18) {
            return NO;
        }
    }
    // 省份代码
    NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
    
    NSString *valueStart2 = [value substringToIndex:2];
    BOOL areaFlag =NO;
    for (NSString *areaCode in areasArray) {
        if ([areaCode isEqualToString:valueStart2]) {
            areaFlag =YES;
            break;
        }
    }
    if (!areaFlag) {
        return false;
    }
    
    NSRegularExpression *regularExpression;
    NSUInteger numberofMatch;
    
    NSInteger year =0;
    switch (length) {
        case 15:{
            year = [[value substringWithRange:NSMakeRange(6,2)] integerValue] +1900;
            
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value
                                                               options:NSMatchingReportProgress
                                                                 range:NSMakeRange(0, value.length)];
            if(numberofMatch >0) {
                return YES;
            }else {
                return NO;
            }
        }
        case 18:{
            year = [[value substringWithRange:NSMakeRange(6,4)] integerValue];
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value
                                                               options:NSMatchingReportProgress
                                                                 range:NSMakeRange(0, value.length)];
            
            
            if(numberofMatch >0) {
                int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                int Y = S %11;
                NSString *M =@"F";
                NSString *JYM =@"10X98765432";
                M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
                    return YES;// 检测ID的校验位
                }else {
                    return NO;
                }
                
            }else {
                return NO;
            }
        }
        default:
            return NO;
    }
}

#pragma mark- 手机号码验证
+ (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

@end
